﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class change_ticket_sell3 : System.Web.UI.Page
    {
        Select SData = new Select();
        string c = "",ticket;
        string[] seat_id = new string[100];
        string[] ticket_print = new string[100];
        int count = 0, point_session, z, user, seat_ID, discount, id_promotion, discount_user;
        float charges = 0, tax = 0, sum = 0, ans_discount = 0;
        DataClasses1DataContext db = new DataClasses1DataContext();

        public DateTime ConvestToDate(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                return new DateTime(years, mount, day);
            }
            catch (Exception)
            {

            }
            return new DateTime(years, mount, day);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 2)
                    {
                        string seat = Session["seat_id_user"].ToString();
                        int AroundID_promotion = Int32.Parse(Session["data_AroundID"].ToString());
                        //z = SData.GetId_ticket();
                        for (int i = 0; i <= seat.Length; i++)
                        {
                            if (i != seat.Length)
                            {
                                if (seat.Substring(i, 1) == ",")
                                {
                                    seat_id[count] = c;
                                    count++;
                                    c = "";
                                }
                                else
                                {
                                    c = c + seat.Substring(i, 1);
                                }
                            }
                            else
                            {
                                seat_id[count] = c;
                                count++;
                            }
                        }
                        point_session = Int32.Parse(Session["point_user"].ToString());
                        string point = @"select * 
                            from Table_Point
                            where Point_id = " + point_session + "";
                        string promotion = @"select *
                                from Table_Promotion
                                inner join Table_Promotion_round
	                                on Table_Promotion.Promotion_id = Table_Promotion_round.Promotion_id
                                inner join Table_Route
	                                on Table_Promotion_round.Route_id = Table_Route.Route_id
                                inner join Table_Around
	                                on Table_Route.Route_id = Table_Around.Route_id
                                where Table_Around.Around_id =  " + AroundID_promotion + " and Table_Promotion.Promotion_status = 1";
                        string identication = Session["Passenger_identification"].ToString();
                        string identication_S = @"select * 
                                from Table_Passenger
                                where Passenger_identification = '" + identication + "'";
                        Session["data_AroundID"] = AroundID_promotion;
                        try
                        {
                            SqlCommand sqlcomm = new SqlCommand();
                            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                            SqlConnection sqlconn = new SqlConnection(Connection);
                            SqlDataAdapter da = new SqlDataAdapter(point, sqlconn);
                            SqlDataAdapter da2 = new SqlDataAdapter(promotion, sqlconn);
                            SqlDataAdapter da_user = new SqlDataAdapter(identication_S, sqlconn);
                            DataTable dt = new DataTable();
                            DataTable dt2 = new DataTable();
                            DataTable dt_user = new DataTable();
                            da.Fill(dt);
                            da2.Fill(dt2);
                            da_user.Fill(dt_user);
                            user = Int32.Parse(dt_user.Rows[0]["Passenger_id"].ToString());
                            string user_Category = @"select Table_Category.Category_Discount
                                from Table_Passenger
                                inner join Table_Category
	                                on Table_Passenger.Category_id = Table_Category.Category_id
                                where Table_Passenger.Passenger_id = " + user + "";
                            SqlDataAdapter da_user_Category = new SqlDataAdapter(user_Category, sqlconn);
                            DataTable dt_user_Category = new DataTable();
                            da_user_Category.Fill(dt_user_Category);
                            discount_user = Int32.Parse(dt_user_Category.Rows[0]["Category_Discount"].ToString());
                            if (dt2.Rows.Count <= 0)
                            {
                                discount = 0;
                                id_promotion = 0;
                            }
                            else
                            {
                                discount = Int32.Parse(dt2.Rows[0]["Promotion_discount"].ToString());
                                id_promotion = Int32.Parse(dt2.Rows[0]["Promotion_id"].ToString());
                            }
                            if (dt.Rows.Count <= 0)
                            {
                                charges = 0;
                                tax = 0;
                            }
                            else
                            {
                                charges = float.Parse(dt.Rows[0]["Point_charges"].ToString());
                                tax = float.Parse(dt.Rows[0]["Point_tax"].ToString());
                            }
                            Session["id_promotion"] = id_promotion;
                            Session["charges"] = charges;
                            Session["tax"] = tax;
                            //////////////////////////////////
                            string tticket = Session["ticket_id"].ToString();
                            string chk_before = @"select *  
                                     FROM Table_Sale
                                     inner join Table_SaleDetail
                                     on Table_Sale.Sale_id = Table_SaleDetail.Sale_id
                                     inner join Table_Promotion
                                     on Table_Sale.Promotion_id = Table_Promotion.Promotion_id
                                     where Table_Sale.Sale_id = '" + tticket + "'";
                            SqlDataAdapter da_chk_before = new SqlDataAdapter(chk_before, sqlconn);
                            DataTable dt_chk_before = new DataTable();
                            da_chk_before.Fill(dt_chk_before);
                            float charges_before = float.Parse(dt_chk_before.Rows[0]["Price_ticket"].ToString());
                            float tax_before = float.Parse(dt_chk_before.Rows[0]["tex"].ToString());
                            float discount_before = Int32.Parse(dt_chk_before.Rows[0]["Promotion_discount"].ToString());
                            ////////////////////////////////
                            ans_discount = (charges * (float)(discount + discount_user)) / 100;
                            sum = (float)count * ((charges - ans_discount) + tax);
                            TextBox1.Text = count.ToString();
                            TextBox2.Text = charges.ToString("0.00");
                            TextBox3.Text = tax.ToString("0.00");
                            TextBox4.Text = sum.ToString("0.00");
                            TextBox5.Text = (discount + discount_user).ToString();
                            float ans_discount_before = (charges_before * (float)(discount_before + discount_user)) / 100;
                            float sum_before = (float)count * ((charges_before - ans_discount_before) + tax_before);
                            if (sum > sum_before)
                            {
                                TextBox6.Text = (sum - sum_before).ToString("0.00");
                            }
                            else
                            {
                                TextBox6.Text = "0";
                            }
                            Session["seat_idd"] = seat_id;
                            Session["countt"] = count;
                            Session["user"] = user;
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int data_Around_chk = Int32.Parse(Session["data_AroundID"].ToString());
            int count = Int32.Parse(Session["countt"].ToString());
            string[] seat_id = (string[])Session["seat_idd"];
            int chk_sale = 0;
            for (int i = 0; i < count; i++)
            {
                string chk_seat = @"select *
                                    from Table_SaleDetail
                                    inner join Table_Sale
                                        on Table_SaleDetail.Sale_id = Table_Sale.Sale_id
                                    where Table_SaleDetail.Around_id =" + data_Around_chk + "and Table_SaleDetail.Seat_no = '" + seat_id[i] + "'and (Table_Sale.SSID =1 or Table_Sale.SSID =2 )";
                SqlCommand sqlcomm = new SqlCommand();
                string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                SqlConnection sqlconn = new SqlConnection(Connection);
                SqlDataAdapter da_chk_seat = new SqlDataAdapter(chk_seat, sqlconn);
                DataTable dt_chk_seat = new DataTable();
                da_chk_seat.Fill(dt_chk_seat);
                if (dt_chk_seat.Rows.Count <= 0)
                {
                    chk_sale = 0;
                }
                else
                {
                    chk_sale++;
                }
                Session["data_AroundID"] = data_Around_chk;
                //discount_user = Int32.Parse(dt_chk_seat.Rows[0]["Category_Discount"].ToString());
            }
            if (chk_sale == 0)
            {
                try
                {
                    for (int i = 0; i < count; i++)
                    {
                        try
                        {
                            Table_Sale sale_save = new Table_Sale();
                            ticket = SData.GetId_tickettt();
                            sale_save.Sale_id = ticket;
                            sale_save.Passenger_id = Int32.Parse(Session["user"].ToString());
                            DateTime a = ConvestToDate(DateTime.Now.ToShortDateString());
                            sale_save.Sale_date = a;
                            string date_time = DateTime.Now.ToString("HH:mm:ss");
                            sale_save.Sale_time = DateTime.Parse(date_time);
                            sale_save.Promotion_id = Int32.Parse(Session["id_promotion"].ToString());
                            //sale_save.Promotion_id = id_promotion;
                            sale_save.Point_id = Int32.Parse(Session["point_user"].ToString());
                            //sale_save.Point_id = point_session;
                            sale_save.Around_id = Int32.Parse(Session["data_AroundID"].ToString());
                            sale_save.SSID = 1;
                            sale_save.percentage = 100;
                            sale_save.Personnel_id =Int32.Parse(Session["login_User"].ToString());
                            db.Table_Sales.InsertOnSubmit(sale_save);
                            db.SubmitChanges();

                            Table_SaleDetail seat = new Table_SaleDetail();
                            seat_ID = SData.GetId_seat();
                            seat.Seat_id = seat_ID;
                            seat.Seat_no = seat_id[i];
                            seat.Sale_id = ticket;
                            seat.Price_ticket = float.Parse(Session["charges"].ToString());
                            //seat.Price_ticket = charges;
                            seat.tex = float.Parse(Session["tax"].ToString());
                            //seat.tex = tax;
                            seat.Around_id = Int32.Parse(Session["data_AroundID"].ToString());
                            db.Table_SaleDetails.InsertOnSubmit(seat);
                            db.SubmitChanges();

                            Table_changeticket xx = new Table_changeticket();
                            Select SID1 = new Select();
                            int z = SID1.GetId_change_ticket();
                            xx.change_id = z;
                            xx.change_date = DateTime.Parse(DateTime.Now.ToString("HH:mm:ss"));
                            string ticket_idd = Session["ticket_id"].ToString();
                            xx.Sale_id = ticket_idd;
                            xx.change_status = false;
                            db.Table_changetickets.InsertOnSubmit(xx);
                            db.SubmitChanges();

                            Table_changeticket x = new Table_changeticket();
                            Select SID = new Select();
                            int idd = SID.GetId_change_ticket();
                            x.change_id = idd;
                            x.change_date = DateTime.Parse(DateTime.Now.ToString("HH:mm:ss"));
                            x.Sale_id = ticket;
                            x.change_status = false;
                            db.Table_changetickets.InsertOnSubmit(x);
                            db.SubmitChanges();

                            var q = from p in db.Table_changetickets
                                    where p.Sale_id == ticket_idd
                                    select p;
                            q.ToList().ForEach(c => c.change_status = false);
                            db.SubmitChanges();

                            var ex = from p in db.Table_Sales
                                     where p.Sale_id == ticket_idd
                                     select p;
                            ex.ToList().ForEach(c => c.SSID = 5);
                            db.SubmitChanges();
                            //Response.Redirect("change_ticket.aspx");
                            ticket_print[i] = ticket.ToString();
                        }
                        catch (Exception)
                        {
                            Response.Redirect("change_ticket.aspx");
                        }

                    }
                    Session["ticket_print"] = ticket_print;
                    //Response.Redirect("ticket_change_new.aspx");
                    Response.Redirect("ticket_change_new.aspx");
                }
                catch (Exception)
                {

                }
            }
            else
            {
                Response.Redirect("change_ticket_sell2.aspx");
            }           
        }
    }
}